package net.xinqushi.design.singleton;

import org.apache.tomcat.util.net.NioSelectorPool;

import java.io.IOException;
import java.nio.channels.Selector;

/**
 * @author xjie
 * @description 懒汉式单例模式是在第一次使用的时候才创建对象
 * @see NioSelectorPool
 */
public class LazySingleton {
    private static volatile Selector SHARED_SELECTOR;

    public static Selector getSharedSelector() throws IOException {
        if (SHARED_SELECTOR == null) { //double check , 配合volatile
            synchronized ( LazySingleton.class ) {
                if ( SHARED_SELECTOR == null )  { //懒汉单例
                    SHARED_SELECTOR = Selector.open(); //单例模式下获取SHARED_SELECTOR
                }
            }
        }
        return  SHARED_SELECTOR;
    }
}
